package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entitys.TbBed;
import com.woniuxy.service.BedService;
import com.woniuxy.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class BedController {
    @Autowired
    private BedService bedService;

    @GetMapping("/bedDetailsByDid/{did}")
    public ResultVO bedDetailsByDid(@PathVariable("did") Integer did){
        List<TbBed> list = bedService.bedDetailsByDid(did);
        return list!=null? ResultVO.success("查询成功",list):ResultVO.failure("查询失败");
    }
    @PostMapping("/bed/getBedmentByHidAndDid")
    public ResultVO getBedmentByHidAndDid(@RequestBody Map<String,Object> map){
        ArrayList<Integer> hidanddid = (ArrayList) map.get("hidanddid");
        Integer hid = hidanddid.get(0);
        Integer did = hidanddid.get(1);
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        PageInfo<TbBed> list = bedService.getFreeBed(hid,did,pageNum,pageSize);
        return list!=null? ResultVO.success("查询成功",list):ResultVO.failure("查询失败");

    }
    @PostMapping("/bed/getAll")
    public ResultVO getAll(@RequestBody Map<String,Object> map){
        Integer pageNum=(Integer) map.get("pageNum");
        Integer pageSize=(Integer) map.get("pageSize");
        String bnum=(String)map.get("bnum");
        Integer hid=null;
        Integer did=null;
        Integer status=null;
        if(map.get("hid")!=null&&map.get("hid")!=""){
            hid = (Integer)map.get("hid");
        }
        if(map.get("status")!=null&&map.get("status")!=""){
          String status1=(String)map.get("status");
          status=Integer.parseInt(status1);
        }
        if(map.get("did")!=null&&map.get("did")!=""){
            did=(Integer)map.get("did");
        }
        PageInfo<TbBed> all = bedService.getAll(pageNum, pageSize, bnum, hid, did, status);
        return all!=null?ResultVO.success("查询成功",all):ResultVO.failure("查询失败");
    }


    /*
    添加维修记录时，模态框表单动态查询床位信息
     */
    @PostMapping("/bed/list")
    public ResultVO getBedByHidAndDid(@RequestBody Map<String,Object> map) {
        Integer hid = (Integer) map.get("addHid");
        Integer did = (Integer) map.get("addDid");
        List<TbBed> bedList = bedService.getBedByHidAndDid(hid, did);
        return bedList != null ? ResultVO.success("success", bedList) : ResultVO.failure("failed");
    }
    /**
     * 根据id查询床位
     * @return
     */
    @GetMapping("/bed/getBedById/{bid}")
    public ResultVO getBedById(@PathVariable Integer bid){
        TbBed bedById = bedService.getBedById(bid);
        if(bedById == null) {
            return ResultVO.failure("没有数据");
        } else {
            return ResultVO.success("查询成功", bedById);
        }

    }

    @GetMapping("/bed/getallBed")
    public ResultVO getallBed(){
        int i = bedService.getallBed();
        return i>=0?ResultVO.success("查询成功",i):ResultVO.failure("查询失败");
    }
}
